package com.icarcom.cn.app;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.StrictMode;
import com.ansen.http.entity.HttpConfig;
import com.ansen.http.net.HTTPCaller;
import com.bumptech.glide.request.target.ViewTarget;
import com.icarcom.cn.R;
import com.icarcom.cn.util.L;
import com.icarcom.cn.util.T;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.cookie.store.PersistentCookieStore;
import com.lzy.okgo.model.HttpHeaders;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import androidx.annotation.RequiresApi;
import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;

/**
 * Created by Administrator on 2020/5/23.
 *
 * jl
 */

public class APP extends MultiDexApplication {
    Context context;
    // 记录是否已经初始化
    public static APP instance;
    public static String registrationId = "";

    /**
     * 打开的activity
     **/
    private List<Activity> activities = new ArrayList<>();


    @RequiresApi(api = Build.VERSION_CODES.GINGERBREAD)
    @Override
    public void onCreate() {
        super.onCreate();
        MultiDex.install(this);

        context = this;
        instance = this;
        //closeAndroidPDialog();

        HttpConfig httpConfig = new HttpConfig();
        httpConfig.setAgent(true);//有代理的情况能不能访问
        httpConfig.setDebug(true);//是否debug模式 如果是debug模式打印log
        httpConfig.setTagName("ansen");//打印log的tagname

        //初始化HTTPCaller类
        HTTPCaller.getInstance().setHttpConfig(httpConfig);

        ViewTarget.setTagId(R.id.glide_tag);
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        T.context = getApplicationContext();
        HttpHeaders headers = new HttpHeaders();
        OkGo.init(this);
        try {
            //以下都不是必须的，根据需要自行选择,一般来说只需要 debug,缓存相关,cookie相关的 就可以了
            OkGo.getInstance()
                    .debug(L.TAG, Level.INFO, true)
                    .setConnectTimeout(15*1000)  //全局的连接超时时间
                    .setReadTimeOut(15*1000)     //全局的读取超时时间
                    .setWriteTimeOut(15*1000)    //全局的写入超时时间
                    .setCacheMode(CacheMode.NO_CACHE)
                    .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
                    .setRetryCount(3)
                    .setCookieStore(new PersistentCookieStore())        //cookie持久化存储，如果cookie不过期，则一直有效
                    .setCertificates()                                  //方法一：信任所有证书,不安全有风险
                    .addCommonHeaders(headers);  //设置全局公共头
        } catch (Exception e) {
            e.printStackTrace();
        }

        //初始化HTTPCaller类
        HTTPCaller.getInstance().setHttpConfig(httpConfig);


       /* Beta.autoInit = true;
        Beta.autoCheckUpgrade = true;
        Beta.initDelay = 1 * 1000;
        Beta.storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        Beta.showInterruptedStrategy = true;
        Beta.canShowUpgradeActs.add(MainActivity.class);
        //Bugly.init(getApplicationContext(), "cce713f2be", false);*/

    }
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    /**
     * 应用退出，结束所有的activity
     */
    public void exit() {

        for (Activity activity : activities) {
            activity.finish();
        }

        //释放内存，退出程序
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    /**
     * 新建了一个activity就加入到集合中
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        activities.add(activity);
    }

    public static APP getInstance() {
        return instance;
    }

}
